const gulp = require('gulp')
const browserSync = require('browser-sync').create()
const sass = require('gulp-sass')(require('sass'))
const concat = require('gulp-concat')
const fileinclude = require('gulp-file-include')

function api (done) {
    gulp.src(['./src/api/*.json', './src/api/**/*.json']).pipe(gulp.dest('./dist/api'))
    done()
}

function statics (done) {
    gulp.src(['./src/static/*','./src/static/**']).pipe(gulp.dest('./dist/static'))
    done()
}

function html (done) {
    //主页
    gulp.src(['./src/view/index.html','./src/view/shoplist.html','./src/view/shopDetail.html','./src/view/login.html','./src/view/regist.html']).pipe(fileinclude({
        prefix: '@@',
        basepath: '@file',
    })).pipe(gulp.dest('./dist'))
    done()
}

function scss (done) {
    gulp.src('./src/public/scss/chunk.scss').pipe(sass().on('error', sass.logError)).pipe(gulp.dest('./dist/css'))
    done()
}

function scripts (done) {
    gulp.src(['./src/public/js/*.js']).pipe(concat('chunk.js')).pipe(gulp.dest('./dist/js'))
    done()
}

function browser (done) {
    browserSync.init({
        server: {
            baseDir: './dist',
            index: 'login.html',
        },
    })
    done()
}

function watch (done) {
    gulp.watch(['./src/static/*','./src/static/**'],gulp.series(statics))
    gulp.watch('./src/view/*.html', gulp.series(html))
    gulp.watch('./src/public/scss/*.scss', gulp.series(scss))
    gulp.watch('./src/public/js/*.js', gulp.series(scripts))
    gulp.watch('./src/components/*.html', gulp.series(scripts,html))
    done()
}

gulp.task('default', gulp.series(statics, html, scss, scripts, browser,watch))